// index.js
const http = require('../../utils/http.js');
const app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		bottomHeight: app.globalData.model.bottomHeight,
		swiperList: ['/images/img.jpg', '/images/img.jpg'],
		statusBarHeight: 0,
		statusBarWidth: 0,
		height: 46,
		sortIndex: 1,
		sortArray: [{
				name: '最新',
				value: 'new',
			},
			{
				name: '热度',
				value: 'hot'
			}
		],
		sortType: 'hot',
		list: [],
		pageCurrent: 1,
		isLoading: false,
		isListMore: true,
		isGgShow: 1,


		isShow: false,
		isInform: 0,
		cert_img: '',
		susend_data: {},
		isnoMore: false,
		isOpenShow: true,
		isLoad: false,

		finderUserNameId: 'sphClyaVhBxzKpy',
		tabCurrentIndex: 0,
		autoplay: true,
		dotsShow: true,
		zbStatus: false
	},
	closeMask() {
		this.setData({
			isShow: false
		})
	},
	changeSwiper(e) {
		if (e.detail.source === 'autoplay' || e.detail.source === 'touch') {
			//根据官方 source 来进行判断swiper的change事件是通过什么来触发的，autoplay是自动轮播。touch是用户手动滑动。其他的就是未知问题。抖动问题主要由于未知问题引起的，所以做了限制，只有在自动轮播和用户主动触发才去改变current值，达到规避了抖动bug
			var index = this.data.tabCurrentIndex;

			this.videoContext = wx.createVideoContext('swiperVideo' + index);
			this.videoContext.pause()
			this.setData({
				tabCurrentIndex: e.detail.current,
				autoplay: true,
				dotsShow: true
			})
		}

	},
	swiperPlay() {
		this.setData({
			autoplay: false,
			dotsShow: false
		})
	},
	acHelp(id){
		http.post('api/user/ac_help',{id}).then(res => {
			if(res.code == 1){
				wx.showToast({
					title: '助力成功~',
					icon: 'none'
				})
			}
		})
	},
	onLoad(options) {
		const {
			statusBarHeight
		} = wx.getSystemInfoSync();
		if(options.shareUserId && options.shareUserId != 'undefined'){
			this.acHelp(options.shareUserId)
		}
		this.setData({
			statusBarHeight,
			height: 46 + statusBarHeight,
			statusBarWidth: wx.getMenuButtonBoundingClientRect().width
		});


	},
	onShow() {
		//判断onLaunch是否执行完毕
		this.getzbStatus();
		if (wx.getStorageSync('isLogin')) {
			this.getData()
		} else {
			this.getOpen();
			app.checkLoginReadyCallback = res => {
				this.getData()
			};
		}

	},
	getData() {
		this.setData({
			isLoad: true
		})
		this.getList(1)
		this.getBanner()
		this.getSuspend()
		this.getInform()
	},
	getOpen() {
		var that = this;
		wx.hideTabBar();
		http.post('api/advert/get_advert', {
			en: 'ad_op_screen',
			li: 0
		}).then(res => {
			if (res.code == 1) {
				that.setData({
					openVideo: res.data[0].videourl,
					isOpenShow: res.data[0].videourl ? 0 : 1,
					isLoad: 1
				})
			} else {
				that.setData({
					isOpenShow: 1,
					isLoad: 1
				})
			}

		})
	},
	onEnded(e) {
		this.setData({
			isOpenShow: true
		})
		wx.showTabBar();
	},
	getGg(name) {
		return http.post('api/advert/get_advert', {
			en: name,
			li: 0
		})
	},
	//关闭开屏广告
	closeOpenShow() {
		this.setData({
			isOpenShow: true,
			openVideo: null
		})
		wx.showTabBar();
	},
	// 轮播广告
	getBanner() {
		this.getGg('ad_ind_banner').then(res => {
			this.setData({
				swiperList: res.data
			})
		})

	},
	goBanner(e) {
		var type = e.currentTarget.dataset.type;
		var url = e.currentTarget.dataset.url;
		if (type == 1) {
			// 小程序
		} else if (type == 2) {
			// 本地
			return wx.navigateTo({
				url
			});
		} else if (type == 3) {
			// H5
			return wx.navigateTo({
				url: '/pages/webview/webview?url=' + url
			});
		}

	},
	// 首页直播广告
	getSuspend() {
		this.getGg('ad_ind_suspend').then(res => {
			this.setData({
				susend_data: res.data[0]
			})
		})
	},
	getzbStatus(){
		console.log('zbjj',this.data.finderUserNameId)
		var id = this.data.finderUserNameId;
		wx.getChannelsLiveInfo({
			finderUserName: id,
			success: (res)=>{
				console.log('zbj',res)
				if(res.status == 2){
					this.setData({
						zbStatus: true
					})
				}else{
					zbStatus: false
				}
			},
			fail:(e)=>{
				console.log('err',e)
			}
		})
	},
	//跳转直播间
	goZbj() {
		wx.openChannelsLive({
			finderUserName: this.data.finderUserNameId,
		})
	},
	//首页证书通知
	getInform() {
		var that = this
		http.post('api/other/inform').then(res => {
			if (res.code == 1) {
				that.setData({
					isShow: res.data.cert ? true : false,
					isInform: res.data.inform,
					cert_img: res.data.cert ? res.data.cert.cert_imgurl : null
				})
			}

		})
	},
	getList(isFirst) {
		var that = this;
		var data = {
			t: this.data.sortType,
			pg: isFirst ? 1 : this.data.pageCurrent
		}
		http.post('api/works/list_index', data).then(res => {
			var pageCurrent = that.data.pageCurrent;
			if (res.data.length > 0) {
				that.setData({
					pageCurrent: isFirst ? 2 : pageCurrent + 1,
					list: isFirst ? res.data : that.data.list.concat(res.data),
					isLoading: false,
					isListMore: true
				})
				return;
			}
			setTimeout(function() {
				that.setData({
					isListMore: false,
					isLoading: false,
					isnoMore: isFirst ? false : true
				})
			}, 10)
		})
	},
	//关闭悬浮广告
	onCloseGg() {
		this.setData({
			isGgShow: false
		})
	},
	//筛选
	bindSortChange: function(e) {
		this.setData({
			sortIndex: e.detail.value,
			sortType: this.data.sortArray[e.detail.value].value
		})
		this.getList(1)
	},
	goPath(e) {
		var url = e.currentTarget.dataset.url;
		wx.navigateTo({
			url,
		})
	},
	onReachBottom() {
		if (this.data.isListMore) {
			this.setData({
				isLoading: true
			})
			this.getList();
		}

	},
	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

		// 刷新逻辑，例如请求数据并更新页面
		wx.showLoading({
			title: '加载中..',
			mask: true
		})
		setTimeout(() => {
			wx.hideLoading();
			// 更新数据
			this.getData()
			// 停止下拉刷新
			wx.stopPullDownRefresh();
		}, 2000);
	}
})